package com.zzusedating.androidapp.tools;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;

public class MyApplication extends Application {
    @SuppressLint("StaticFieldLeak")
    private static Context context;
    private static JSONObject basicProfile;
    private static JSONObject detailProfile;
    private static Toast mToast;

    private static final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
    private static OkHttpClient client = new OkHttpClient.Builder()
            .cookieJar(new CookieJar() {
                @Override
                public void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {
                    cookieStore.put(url.host(), cookies);
                }

                @Override
                public List<Cookie> loadForRequest(@NonNull HttpUrl url) {
                    List<Cookie> cookies = cookieStore.get(url.host());
                    return cookies != null ? cookies : new ArrayList<Cookie>();
                }
            })
            .build();

    public static JSONObject getBasicProfile() {
        return basicProfile;
    }

    public static JSONObject getDetailProfile() {
        return detailProfile;
    }

    public static void setDetailProfile(JSONObject detailProfile) {
        MyApplication.detailProfile = detailProfile;
    }

    public static void setBasicProfile(JSONObject basicProfile) {
        MyApplication.basicProfile = basicProfile;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static OkHttpClient getClient() {
        return client;
    }

    public static Context getContext() {
        return context;
    }

    public static void Toast(String info){
        Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
    }
}
